package com.smartkey.activity;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.project.smartkey.R;
import com.smartkey.adapter.AdapterAppPick;
import com.smartkey.bean.AppInfo;
import com.smartkey.utils.AppUtil;
import com.smartkey.utils.ClickType;
import com.smartkey.utils.SetListViewHeightUtil;

public class AppPickWithDefaultActivty extends BaseActivity implements
		OnClickListener {

	private TextView title;
	private ImageView icon;

	private ListView listApp;
	private AdapterAppPick adapterAppPickWithDefault;

	private RelativeLayout rlMoreApp;

	private ClickType clickType;
	
	private AppInfo appInfo;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sk_activity_apppickwithdefault);
		initParam();
		initView();
	}

	@Override
	protected void onResume() {
		super.onResume();
		resumeData();
		refreshListView();
	}

	private void initParam() {
		Intent intent = getIntent();
		if (intent == null) {
			return;
		}

		String clickTypeStr = intent.getStringExtra("clickType");
		try {
			clickType = ClickType.valueOf(clickTypeStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void initView() {
		title = (TextView) findViewById(R.id.txt_title);
		icon = (ImageView) findViewById(R.id.img_icon);
		listApp = (ListView) findViewById(R.id.list_app);
		rlMoreApp = (RelativeLayout) findViewById(R.id.rl_get_more_app);
		rlMoreApp.setOnClickListener(this);
		listApp.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				appInfo = (AppInfo) adapterAppPickWithDefault
						.getItem(arg2);
				if(appInfo.packageName.equals(AppInfo.APPID.DIAL.getValue())){//电话设置
					Intent intent = new Intent(AppPickWithDefaultActivty.this, CallSettingActivity.class);
					startActivityForResult(intent, 101);
					return;
				}
				if(appInfo.packageName.equals(AppInfo.APPID.SENDMESSAGE.getValue())){//短信设置
					Intent intent = new Intent(AppPickWithDefaultActivty.this, SMSSettingActivity.class);
					startActivityForResult(intent, 101);
					return;
				}
				
				AppUtil.setCheckedApp(AppPickWithDefaultActivty.this, appInfo,
						clickType);
				refreshListView();
			}
		});
		findViewById(R.id.img_back).setOnClickListener(this);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode==101&&resultCode==101){
			AppUtil.setCheckedApp(AppPickWithDefaultActivty.this, appInfo,
					clickType);
			System.out.println("########### onActivityResult");
			refreshListView();
		}
	}

	private void resumeData() {
		switch (clickType) {
		case CLICK:
			title.setText(R.string.label_click);
			icon.setBackgroundResource(R.drawable.sapp_click);
			break;
		case DCLICK:
			title.setText(R.string.label_dclick);
			icon.setBackgroundResource(R.drawable.sapp_dclick);
			break;
		case TCLICK:
			title.setText(R.string.label_tclick);
			icon.setBackgroundResource(R.drawable.sapp_tclick);
			break;
		}
		refreshListView();
	}

	private void refreshListView() {
		adapterAppPickWithDefault = new AdapterAppPick(this, clickType, true);
		listApp.setAdapter(adapterAppPickWithDefault);
		adapterAppPickWithDefault.notifyDataSetChanged();
		// 调整listview高度
		SetListViewHeightUtil.setListViewHeightBasedOnChildren(listApp);
	}

	@Override
	public void onClick(View v) {
		if (R.id.rl_get_more_app == v.getId()) {
			// 跳转到用户自安装应用列表页面
			Intent intent = new Intent(this, AppPickUserCustomerActivity.class);
			intent.putExtra("clickType", clickType.getValue());
			startActivity(intent);
		} else if (R.id.img_back == v.getId()) {
			finish();
		}
	}

}
